﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace IQ.WinComponents
{
    public partial class FilterGridHeader : UserControl
    {
		#region # Constructor # 
        public FilterGridHeader()
        {
            InitializeComponent();
            btnSetCols.Visible = false;
        }
		#endregion
		
		#region - Private variables - 
        private grdFilter filterGrid;
        private bool boxHeading;
        private int startLeft = 15;
        private int boxWidth = 150;
        #endregion 

		#region - Private methods - 
        private void setCosmetics()
        {
            gbpHeader.Text = boxHeading ? "Filter" : "";
            lblHeading.Left = boxHeading ? -200 : startLeft;
            Label_Field.Left = boxHeading ? startLeft : lblHeading.Left + lblHeading.Width + 8;
            Text_Filter.Width = boxWidth;
            Text_Filter.Left = Label_Field.Left;
        }
        private void setButtonColor()
        {
            if (filterGrid != null &&
                filterGrid.Columns.Count != filterGrid.Columns.GetColumnCount(DataGridViewElementStates.Visible))
                btnSetCols.BackColor = Color.Pink;
            else
                btnSetCols.BackColor = Color.AliceBlue;
        }
		#endregion 
		
		#region - Public properties / variables - 
        public bool GroupBoxHeading
        {
            get { return boxHeading; }
            set
            {
                boxHeading = value;
                setCosmetics();
            }
        }
        public int LeftStart
        {
            get { return startLeft; }
            set
            {
                startLeft = value;
                setCosmetics();
            }
        }
        public grdFilter FilterGrid
        {
            get { return filterGrid; }
            set
            {
                filterGrid = value;
                if (filterGrid != null)
                {
                    filterGrid.TxtFilter = Text_Filter;
                    filterGrid.SelectedFieldLabel = Label_Field;
                    setButtonColor();
                }
            }
        }
        public int FilterBoxSize
        {
            get { return boxWidth; }
            set
            {
                boxWidth = value;
                setCosmetics();
            }
        }
        public bool ChooseColumns
        {
            get { return btnSetCols.Visible; }
            set { btnSetCols.Visible = value; }
        }
        #endregion

        #region - Control Methods -
        // This region is for event handler methods that are called knowingly by the user.
        // Such as a button being clicked.
        private void btnSetCols_Click(object sender, EventArgs e)
        {
            /*
            Button btn = (Button)sender;
            Form frm = btn.FindForm();
            Type frmType = frm.GetType();
            this.Label_Field.Text = frmType.Namespace + "," + frm.Name + "," + this.Name;
            //
            */
            ContextMenuStrip popCol = new ContextMenuStrip();
            btnSetCols.ContextMenuStrip = popCol;
            popCol.AutoClose = false;
            foreach (DataGridViewColumn col in this.filterGrid.Columns)
            {
                ToolStripMenuItem tsi = new ToolStripMenuItem();
                tsi.Name = "tsi" + col.Name;
                tsi.CheckOnClick = true;
                tsi.Text = col.HeaderText;
                tsi.Checked = col.Visible;
                tsi.Tag = col;
                popCol.Items.Add(tsi);
            }
            ToolStripMenuItem tsiClose = new ToolStripMenuItem();
            tsiClose.Name = "tsiClose" + this.Name;
            tsiClose.Text = "Close";
            tsiClose.Click += btnCol_Menu_Close;
            popCol.Items.Add(tsiClose);
            popCol.Show(Cursor.Position);
        }
        private void btnCol_Menu_Close(object sender, EventArgs e)
        {
            foreach (ToolStripMenuItem itm in btnSetCols.ContextMenuStrip.Items)
            {
                DataGridViewColumn col = (DataGridViewColumn)itm.Tag;
                if (col != null)
                    col.Visible = itm.Checked;
            }
            setButtonColor();
            btnSetCols.ContextMenuStrip.Close();
        }
        #endregion 
    }
}
